package com.heima.xxljob.job;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class HelloJob {

    /**
     * 轮询:一次只发送一个请求. 按照节点顺序,轮流发
     * 分片广播: 向所有节点都发送请求
     */
//    @XxlJob("demoJobHandler")
//    public void helloJob() {
//        System.out.println("入门案例简单任务执行了。。。。" + new Date());
//
//    }


    @XxlJob("shardingJobHandler")
    public void shardingJobHandler() {
        //分片的参数
        int shardIndex = XxlJobHelper.getShardIndex(); //分片索引
        int shardTotal = XxlJobHelper.getShardTotal(); //分片总数

        //业务逻辑
        List<Integer> list = getList();
        for (Integer integer : list) {
            if (integer % shardTotal == shardIndex) {
                System.out.println("当前第" + shardIndex + "分片执行了，任务项为：" + integer);
            }
        }
    }

    public List<Integer> getList() {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            list.add(i);
        }
        return list;
    }


}